package com.kylin.module.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.CatDict;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.module.admin.business.ManageCatDictBlogic;
import com.kylin.module.admin.form.CatDictForm;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

/**
 * 后台管理接口：管理系统的属性字典种类
 * @author lzh
 *
 */
public class ManageCatDictAction extends BaseDispatchAction implements BaseActionInterface {

	protected static Logger log = Logger.getLogger(ManageCatDictAction.class);

	public ActionForward add(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始保存属性字典 : "+request.getParameter("name"));
		CatDictForm form = (CatDictForm)actionForm;
		String forward = "addSucc";
		try{
			form.setLanguage(getSessionLocale(request).toString());
			ManageCatDictBlogic bean = new ManageCatDictBlogic();
			bean.addCatDict(form);
			log.info("增加属性字典成功："+ form.getName());
		}catch (Exception e) {
			forward = doException( request, e, "addBusinessException");
		}
		return mapping.findForward(forward);
	}

	public ActionForward bfUpdate(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查看修改属性表 : "+request.getParameter("name"));
		CatDictForm form = (CatDictForm)actionForm;
		String forward = "bfUpdate";
		String id = request.getParameter("id");
		try{
			ManageCatDictBlogic bean = new ManageCatDictBlogic();
			CatDict cat = bean.getCatDictById(new Long(id));
			form.setId(cat.getId());
			form.setName(cat.getName());
			if(cat.getHassub() != null && cat.getHassub().intValue() == 1)
				form.setHasSub("on");
			form.setLanguage(cat.getLanguage());
			form.setOrdering(cat.getOrdering());
		}catch (Exception e) {
			forward = doException( request, e, "bfUpdateBusinessException");
		}
		return mapping.findForward(forward);
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String forward = "updateSucc";
		CatDictForm form = (CatDictForm)actionForm;
		try{
			ManageCatDictBlogic bean = new ManageCatDictBlogic();
			form.setLanguage(this.getSessionLocale(request).toString());
			System.out.println("Language=========="+form.getLanguage());
			bean.updateCatDict(form);
		}catch (Exception e) {
			forward = doException( request, e, "updateBusinessException");
		}
		return mapping.findForward(forward);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String forward = "list";
		int pageNum = 1;
		String id = request.getParameter("id");
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		if (request.getParameter("page") != null) {
			pageNum = Integer.parseInt(request.getParameter("page"));
		}
		try{
			ManageCatDictBlogic bean = new ManageCatDictBlogic();
			bean.deleteCatDictById(new Long(id));
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				cinfo = new CriteriaInfo(CatDict.class, null, Order.asc("ordering"));
				searchId = this.addUserCacheCriteriaInfo(request, cinfo);
			}
			cinfo = getUserCacheCriteriaInfo(request, searchId);
			DisplayPage page = bean.getCatDictList(pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);

		}catch (Exception e) {
			forward = doException( request, e, "deleteBusinessException");
		}
		return mapping.findForward(forward);
	}

	public ActionForward search(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查询属性表 : searchId="+request.getParameter("searchId"));
		String forward = "list";
		int pageNum = 1;
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				cinfo = new CriteriaInfo(CatDict.class, null, Order.asc("ordering"));		
				addUserCacheCriteriaInfo(request, cinfo);
			}else{
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			ManageCatDictBlogic bean = new ManageCatDictBlogic();
			DisplayPage page = bean.getCatDictList(pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		}catch (Exception e) {
			doException( request, e, "searchBusinessException");
		}
		return mapping.findForward(forward);
	}

}